<?php
/**
 * Created by PhpStorm.
 * User: 123
 * Date: 2020/7/27
 * Time: 15:22
 */

namespace app\admin\controller;

use app\api\controller\SmsCode;
use app\service\ExpressService;
use app\service\OrderService;
use base\Yunsms;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use think\Db;

/**
 * 订单管理.
 * @author   juzi
 * @blog    https://blog.csdn.net/juziaixiao
 * @version  0.0.1
 * @datetime 2020年7月22日10:57:35
 */
class Order extends Common
{
    /**
     * 构造方法.
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  1.0.0
     * @datetime 2020年7月25日07:38:52
     */
    public function __construct()
    {
        // 调用父类前置方法
        parent::__construct();

        $this->IsLogin();
    }
    /**
     * [Index 订单统计列表].
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  1.0.0
     * @datetime 2020年7月22日10:58:48
     */
    public function Index()
    {
//        $re=Db::name('member_orderdetail')->select();
//        foreach ($re as $v) {
//          // dump($v['price']*$v['buy_num']) ;
//            $a=$v['price']*$v['buy_num'];
//            Db::name('bizpro')->where('id='.$v['bizpro_id'])->setInc('total_sale',$a);
//        }
//       // exit;

        if ('ajax' != input('get.action')) {
            $this->assign('id_arr', input('get.id_arr'));
            return $this->fetch();
        }
        else {
           // 参数
            $params = input();
            // 条件
            $params['param']['id_arr']=empty($params['id_arr'])?'':$params['id_arr'];
            $where = OrderService::OrderListWhere($params['param']);
            $where[]=['mod.order_status','>',1];
            //dump($where);exit;
            $data_params = [
                'where' => $where,
                'page' => true,
                'number' => 10,
                'group'=>'mod.bizpro_id',
                'orderby'=>'member_order.create_time',
                'field'=>'mod.buy_price,count(bizpro_id) bizpro_num,bizpro_id,mod.order_status,mod.id,mod.price,mod.bizpro_spec_value,mo.create_time, mod.order_id,mod.bizpro_name,mod.bizpro_number,mod.buy_num,mod.supplier_id,mod.orderdetail_status,s.supplier_name'
            ];
            $re=OrderService::OrderList($data_params);
            $total= OrderService::DataTotal('mod.bizpro_id',$where);
            $total_arr = OrderService::ToSendDataTotal();

            return ['code' => 0, 'msg' => '', 'count' => $total, 'data' => $re['data'],'tosendprice'=>$total_arr['tosendprice'],'tosendcate'=>$total_arr['tosendcate'],'tosendnum'=>$total_arr['tosendnum']];
        }
    }


    /**
     * [OrderDetail 订单详情]
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  1.0.0
     * @datetime 2020年7月22日10:57:59
     */
    public function OrderDetail(){
        $params = input();

        // 获取列表
        $data_params = [
            'm' => 0,
            'n' => 1,
            'page' => false,
            'where' => ['mod.bizpro_id' => intval($params['bizpro_id'])],
            'field'=>'mod.id,mod.price,mod.bizpro_spec_value,mo.create_time, mod.order_id,mod.bizpro_name,mod.bizpro_number,mod.buy_num,mod.supplier_id,mod.orderdetail_status,s.supplier_name'
        ];

        $ret=OrderService::OrderList($data_params);

        $data = empty($ret['data'][0]) ? [] : $ret['data'][0];

        $this->assign('res', $data);

        return $this->fetch();
    }


    /**
     * [删除数据].
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年7月21日17:02:36
     */
    public static function DelInfo()
    {
        $params=input();
       // ($params);exit;
        $params['table']='member_orderdetail';
        $params['errorcode']='50000';
        $params['msg']='删除订单失败';
        $params['soft']=true;
        $params['soft_arr']=['is_delete'=>1];
        OrderService::DelInfo($params);
        return DataReturn('删除成功', 0);
    }


    /**
     * [SaveOrderDetail 编辑].
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年6月23日09:57:30
     */
    public function SaveOrderDetail()
    {

        // 是否ajax
        if (!IS_AJAX) {
            return $this->error('非法访问');
        }
        // 开始操作
        $params = $this->data_post;
        //数据校验
        OrderService::VaryData($params);
        //整理数据
        OrderService::SaveMod($params);
        return DataReturn('保存成功', 0);
    }

    /**
     * [MkOrder 下单页面].
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年6月23日09:57:30
     */
    public function MkOrder()
    {
        $params = input();

        // 获取列表
        $data_params = [
            'page' => false,
            'where' => [['mod.is_delete','=',2],['mod.order_status','>',1],['bizpro_id','=',intval($params['bizpro_id'])]],
                //['mod.bizpro_id' => intval($params['bizpro_id'])],
            'field'=>'mod.id,mo.order_number,mo.receive_name,mo.receive_phone,mod.bizpro_spec_value,mo.province,mo.city,mo.area,mo.address,mod.bizpro_number,mod.bizpro_name,mod.price,mod.orderdetail_status,mod.buy_num,mod.buy_price pursh_price,mod.order_id,mod.bizpro_id'
        ];
        $ret=OrderService::OrderList($data_params);
        $where=[
            ['mod.is_delete','=',2],
            ['mod.order_status','>',1],
            ['bizpro_id','=',intval($params['bizpro_id'])]
        ];

        $total_arr = OrderService::ToSendDataTotal($where);
        $this->assign('total_arr', $total_arr);
        $this->assign('res', $ret['data']);
        $this->assign('bizpro_id', $params['bizpro_id']);
        return $this->fetch();
    }

    /**
     * [DoMk 执行下单].
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年6月23日09:57:30
     */
    public function DoMk()
    {
        // 是否ajax
        if (!IS_AJAX)
        {
            return $this->error('非法访问');
        }
        // 开始操作
        $params = $this->data_post;

        $id=explode(',',$params['id']);
        array_pop($id);
        $data=[
            'where'=> [['id','in',$id]],
            'update'=>['orderdetail_status'=>1],
            'table' =>'member_orderdetail',
            'msg'=>'修改订单详情失败',
            'errorCode'=>'4003',
        ];
        //保存数据
        OrderService::SaveOrder($data);
        //供应商结款
        OrderService::SaveSupPrice(['id'=>$id[0]]);
        return DataReturn('保存成功', 0);
    }

    /**
     * [ActionIndex 操作订单].
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年6月23日09:57:30
     */
    public function ActionIndex()
    {
        if ('ajax' != input('get.action')) {

            return $this->fetch();
        }
        else {
            // 参数
            $params = $this->data_post;
            $params['param']['orderdetail_status']='1';
            $params['param']['order_status']=isset($params['param']['order_status'])?$params['param']['order_status']:2;
            // 条件
            $where = OrderService::OrderListWhere($params['param']);
            //dump($where);exit;
            $data_params = [
                'where' => $where,
                'page' => true,
                'number' => 10,
                'orderby'=>'member_order.create_time',
                'field'=>'mod.order_status,mo.member_id,m.phone,mo.receive_phone,mo.receive_name,mod.id mod_id,mo.province,mo.city,mo.area,mo.address,mod.update_time,mod.price,mod.order_status'
            ];

            $re=OrderService::OrderList($data_params);

            $total= OrderService::DataTotal('',$where);

            return ['code' => 0, 'msg' => '', 'count' => $total, 'data' => $re['data'],];
        }
    }
    /**
     * [Actiondetail 操作订单详情].
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年6月23日09:57:30
     */
    public function ActionDetail(){
        $params = $this->data_get;
        // 公共数据校验请求参数
        $p = [
            [
                'checked_type' => 'empty',
                'key_name' => 'id',
                'error_msg' => 'id不能为空',
                'error_code' => 50001,
            ],
        ];
        $ret=ParamsChecked($params, $p);
        if(true !== $ret)
        {
            $error_arr = explode(',', $ret);
            throw new BaseException(['code'=>403 ,'errorCode'=>$error_arr[1],'msg'=>$error_arr[0],'status'=>false,'debug'=>false ]);
        }
        $field='mod.supplier_id,mod.bizpro_number,mod.express_code,mod.logistic_id,mod.id mod_id,mo.receive_phone,mo.receive_name,mo.member_id,mo.province,mo.city,mo.area,mo.address,mo.pay_price,mo.cheap_price,mo.total_price,mo.pay_type,mo.pay_time,mo.order_number';
        // 获取列表
        $data_params = [
            'm' => 0,
            'n' => 1,
            'page' => false,
            'where' => ['mod.id' => intval($params['id'])],
            'field'=>$field,
        ];
        $ret=OrderService::OrderList($data_params);

        $this->assign('res',$ret['data'][0]);
        return $this->fetch();
    }

    /**
     * [ExportData 订单操作-导出订单].
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年6月23日09:57:30
     */
    public function ExportData(){

        $params = $this->data_get;
        $field='mod.id,mo.receive_phone,mo.receive_name,mo.province,mo.city,mo.area,mo.address,mo.pay_price,mo.total_price,mo.pay_time,mod.bizpro_name,mod.bizpro_number,mod.bizpro_spec_value,mod.buy_num,mod.price,mo.total_price';

        // 获取列表
        $data_params = [
            'm' => 0,
            'n' => 1,
            'page' => false,
            'where' => ['mod.id' => intval($params['id'])],
            'field'=>$field,
        ];
        $ret=OrderService::OrderList($data_params);

        $excel = new \base\Excel(
            ['filename' => '云盟商桥用户订单',
                'title' => lang('excel_action_detail_order_list'),
                'data' => $ret['data'],
                'msg' => '没有相关数据']
        );
        $excel->Exporttest();
       //$excel->Export();
    }

    /**
     * [MkExportData 订单统计-导出订单].
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年6月23日09:57:30
     */
    public function MkExportData(){
        $params = input();

        // 获取列表
        $data_params = [
            'page' => false,
            'where' => ['mod.bizpro_id' => intval($params['bizpro_id'])],
            'field'=>'mod.id,mo.order_number,mo.receive_name,mo.receive_phone,mod.bizpro_spec_value,mo.province,mo.city,mo.area,mo.address,mod.bizpro_number,mod.bizpro_name,mod.price,mod.orderdetail_status,mod.buy_num,mod.buy_price pursh_price,mod.order_id,mod.bizpro_id'
        ];

        $ret=OrderService::OrderList($data_params);
        dump($ret['data']);exit;
        $excel = new \base\Excel([
            'filename' => '云盟商桥商品订单',
            'title' => lang('excel_mk_order_list'),
            'data' => $ret['data'], 'msg' => '没有相关数据'
        ]);

        $excel->Exporttest();
    }

    /**
     * [SendDetail 点击发货页面].
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年6月23日09:57:30
     */
    public function SendDetail()
    {
        $data_params  = [
            'where' => [['id','>',0]],
            'page' => false,
            'field' => '*',
            'table'=>'express',
        ];

        $re= ExpressService::DataList($data_params);
        $this->assign('express',$re);
        $params = $this->data_get;
        $field='mod.id mod_id,mod.logistic_id,mod.express_code,mod.supplier_id';
        // 获取列表
        $data_params = [
            'm' => 0,
            'n' => 1,
            'page' => false,
            'where' => ['mod.id' => intval($params['id'])],
            'field'=>$field,
        ];
        $res=OrderService::OrderList($data_params);

        $this->assign('info',$res['data'][0]);
        return $this->fetch('send_detail');
    }
    /**
     * [SaveSend 执行发货].
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年6月23日09:57:30
     */
    public function SaveSend()
    {
        $params=$this->data_post;

        $params['order_status']=3;


        $data['where']=['id'=>$params['id']];
        $data['update']=$params;
        $data['table']='member_orderdetail';
        $data['msg']='修改订单详情失败';
        $data['errorCode']='4004';
        //整理数据
        OrderService::SaveOrder($data);
        $data['where']=['id'=>Db::name('member_orderdetail')->where('id='.$params['id'])->value('order_id')];
        $data['update']=['order_status'=>3];
        $data['table']='member_order';
        $data['msg']='修改订单失败';
        $data['errorCode']='4003';
        OrderService::SaveOrder($data);        //OrderService::SaveMod($params);


        return DataReturn('保存成功', 0);

    }


    /**
     * [DoReceive 确认收货].
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年6月23日09:57:30
     */
    public function DoReceive()
    {
        $params=$this->data_post;
        $data['where']=['id'=>$params['id']];
        $data['update']=$params;
        $data['table']='member_orderdetail';
        $data['msg']='修改订单详情失败';
        $data['errorCode']='4004';
        //整理数据
        OrderService::SaveOrder($data);
        return DataReturn('保存成功', 0);

    } /**
 * [DoReceive 修改订单详情 订单数量 进货价格].
 * @author   juzi
 * @blog    https://blog.csdn.net/juziaixiao
 * @version 1.0.0
 * @date    2020年6月23日09:57:30
 */
    function orderWrite(){
        $info = input("post.");
        if(!empty($info)){
            if($info['mark']=='num'){
                #修改数量
                $re = Db::table("member_orderdetail")
                    ->where(array('order_id'=>$info['orderId'],'bizpro_id'=>$info['bizproId']))
                    ->update(array('buy_num'=>$info['buy_num']));

            }else{
                #修改价格
                $re = Db::table("member_orderdetail")
                    ->where(array('order_id'=>$info['orderId'],'bizpro_id'=>$info['bizproId']))
                    ->update(array('buy_price'=>$info['purshPrice']));
            }
            if($re){
                return array('status'=>true);
            }
        }

    }



    public function aa(){

        $reader= new Xls();
        $spreadsheet = $reader->load(ROOT_PATH.'123.xls');
        $sheet = $spreadsheet->getActiveSheet();
        $row_count = $sheet->getHighestRow();//取得总行数
        $content='';
        for ($row = 2; $row <= $row_count; $row++) {
            $user=$sheet->getCell('C'.$row)->getValue();
            $lenth=$sheet->getCell('J'.$row)->getValue();
            $date=$sheet->getCell('K'.$row)->getValue();
            $date=intval(substr($date,0,4)).'年'.intval(substr($date,4,2)).'月'.intval(substr($date,6)).'日';
            $project=$sheet->getCell('L'.$row)->getValue();
            $fee=substr($sheet->getCell('M'.$row)->getValue(),0,3);
            $phone=$sheet->getCell('D'.$row)->getValue();
            $content .=$row.'--'.$phone.'---尊敬的'.$user.'先生/女士您好，我是长城汽车朝阳凯信哈弗4S店服务顾问刘忠悦，感谢您长期以来对我店的支持与信赖，结合您上次维保进站情况，再次对您下次保养事项温馨提示如下：1.您的爱车下次保养里程为'.$lenth.'公里，预计保养时间为'.$date.'，预计维保项目为'.$project.'预计费用为'.$fee.'；2.预约方式：日常您有任何维保服务需求，可通过我店维保预约热线:0421-2724999,或哈弗智家APP、哈弗车主微信小程序等线上平台预约，我店将竭诚为您服务，祝您用车愉快。'."\r\n\r\n";
            //$re=(new SmsCode())->Ysms('13080745917',$content);
            //sleep(3);

        }
        dump($content);

    }
}